﻿using System;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace Guru.Web.Controls.Asp
{
    public class ImageFlowViewer : WebControl, INamingContainer
    {
        public ImageFlowViewer(): base() 
        {
            AnimationSpeed = 50;
            AspectRatio = 1.964;
            AllowButtons = false;
            AllowCaptions = true;
            Circular = false;
            AllowGlideToStartID = true;            
            ImageFocusM = 1.0;
            ImageFocusMax = 4;
            AllowImageScaling = true;
            ImagesHeight = 0.67;
            ImagesM = 1.0;
            OnClientClick = string.Empty;
            Opacity = false;
            OpacityArray = new int[] { 10, 8, 6, 4, 2 };
            PercentLandscape = 118;
            PercentOther = 100;
            AllowPreloadImages = true;
            AllowReflections = false;
            ReflectionGET = string.Empty;
            ReflectionP = 0.5;
            AllowReflectionPNG = false;
            ReflectPath = string.Empty;
            ScrollbarP = 0.6;
            AllowSlider = true;
            SliderWidth = 14;
            AllowSlideshow = false;
            SlideshowSpeed = 1500;
            AutoplaySlideshow = false;
            StartIndex = 1;
            AllowAnimationOnStart = false;
            XStep = 150;      
        }

        #region Properties
        public string ReflectionFolder
        {
            get;
            set;
        }

        /// <summary>
        /// Animation speed in ms
        /// </summary>
        public int AnimationSpeed
        {
            get;
            set;
        }
        /// <summary>
        /// Aspect ratio of the ImageFlow container (width divided by height)
        /// </summary>
        public double AspectRatio
        {
            get;
            set;
        }
        /// <summary>
        /// Toggle navigation buttons
        /// </summary>
        public bool AllowButtons
        {
            get;
            set;
        }
        /// <summary>
        /// Disables / enables the captions
        /// </summary>
        public bool AllowCaptions
        {
            get;
            set;
        }
        /// <summary>
        /// Toggle circular rotation
        /// </summary>
        public bool Circular
        {
            get;
            set;
        }
        /// <summary>
        /// Toggle glide animation to start ID
        /// </summary>
        public bool AllowGlideToStartID
        {
            get;
            set;
        }
        protected string ImageFlowID
        {
            get
            {
                return this.ClientID + "_Images";
            }
        }
        /// <summary>
        /// Multiplicator for the focussed image size in percent
        /// </summary>
        public double ImageFocusM
        {
            get;
            set;
        }
        /// <summary>
        /// Maximum number of images on each side of the focussed one
        /// </summary>
        public int ImageFocusMax
        {
            get;
            set;
        }
        /// <summary>
        /// Toggle image scaling
        /// </summary>
        public bool AllowImageScaling
        {
            get;
            set;
        }
        /// <summary>
        /// Height of the images div container in percent
        /// </summary>
        public double ImagesHeight
        {
            get;
            set;
        }
        /// <summary>
        /// Multiplicator for all images in percent
        /// </summary>
        public double ImagesM
        {
            get;
            set;
        }
        /// <summary>
        /// Name of function that is called onclick of the focussed image
        /// </summary>
        public string OnClientClick
        {
            get;
            set;
        }
        /// <summary>
        /// Disables / enables image opacity
        /// </summary>
        public bool Opacity
        {
            get;
            set;
        }
        /// <summary>
        /// Image opacity range - first value is for the focussed image (0 = 100% opacity, 10 = 0% opacity) 
        /// </summary>
        public int[] OpacityArray
        {
            get;
            set;
        }
        /// <summary>
        /// Scale landscape format
        /// </summary>
        public int PercentLandscape
        {
            get;
            set;
        }
        /// <summary>
        /// Scale portrait and square format
        /// </summary>
        public int PercentOther
        {
            get;
            set;
        }
        /// <summary>
        /// Disables / enables the loading bar and image preloading
        /// </summary>
        public bool AllowPreloadImages
        {
            get;
            set;
        }
        /// <summary>
        /// Disables / enables the reflections
        /// </summary>
        public bool AllowReflections
        {
            get;
            set;
        }
        /// <summary>
        /// Passes variables via the GET method to the reflect2.php and reflect3.php script
        /// </summary>
        public string ReflectionGET
        {
            get;
            set;
        }
        /// <summary>
        /// Reflection height in % of the source image (1.0 = 100%, 0 = 0%)
        /// </summary>
        public double ReflectionP
        {
            get;
            set;
        }
        /// <summary>
        /// Switches from the reflect2.php to the reflect3.php for PNG transparency support
        /// </summary>
        public bool AllowReflectionPNG
        {
            get;
            set;
        }
        /// <summary>
        /// Path to the reflect_.php script
        /// </summary>
        public string ReflectPath
        {
            get;
            set;
        }
        /// <summary>
        /// Width of the scrollbar in percent
        /// </summary>
        public double ScrollbarP
        {
            get;
            set;
        }
        /// <summary>
        /// Disables / enables the scrollbar
        /// </summary>
        public bool AllowSlider
        {
            get;
            set;
        }
        /// <summary>
        /// Width of the slider in px
        /// </summary>
        public int SliderWidth
        {
            get;
            set;
        }
        /// <summary>
        /// Toggle slideshow
        /// </summary>
        public bool AllowSlideshow
        {
            get;
            set;
        }
        /// <summary>
        /// Time between slides in ms
        /// </summary>
        public int SlideshowSpeed
        {
            get;
            set;
        }
        /// <summary>
        /// Toggle automatic slideshow play on startup
        /// </summary>
        public bool AutoplaySlideshow
        {
            get;
            set;
        }
        /// <summary>
        /// Glide to this image number on startup
        /// </summary>
        public int StartIndex
        {
            get;
            set;
        }
        /// <summary>
        /// Animate images moving in from the right on startup
        /// </summary>
        public bool AllowAnimationOnStart
        {
            get;
            set;
        }
        /// <summary>
        /// Step width on the x-axis in px
        /// </summary>
        public int XStep
        {
            get;
            set;
        }
        #endregion

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            string initScript = @"$(document).ready(function() { 
                                            initImagesFlow();
                                        });
                                function initImagesFlow(){
                                    var instanceOne = new ImageFlow(); 
                                    instanceOne.init({ [PRAMETERS] });
                                }";
            string parameters = @"animationSpeed: {0}
                                ,aspectRatio: {1}
                                ,buttons: {2}
                                ,captions: {3}
                                ,circular: {4}
                                ,glideToStartID: {5}                               
                                ,ImageFlowID: '{6}'
                                ,imageFocusM: {7}
                                ,imageFocusMax: {8}                               
                                ,imageScaling: {9}
                                ,imagesHeight: {10}
                                ,imagesM: {11}
                                ,onClick: function()[OPEN]{12}[CLOSED] 
                                ,opacity: {13}
                                ,opacityArray: new Array({14})
                                ,percentLandscape: {15}
                                ,percentOther: {16}
                                ,preloadImages: {17}
                                ,reflections: {18}
                                ,reflectionGET: '{19}'
                                ,reflectionP: {20}
                                ,reflectionPNG: {21}
                                ,reflectPath: '{22}'
                                ,scrollbarP: {23}
                                ,slider: {24}                                
                                ,sliderWidth: {25}
                                ,slideshow: {26}
                                ,slideshowSpeed: {27}
                                ,slideshowAutoplay: {28}
                                ,startID: {29}
                                ,startAnimation: {30}
                                ,xStep: {31}";

            string opacityArray = string.Empty;
            foreach (int item in this.OpacityArray)
            {
                opacityArray = opacityArray + item + ",";
            }
            opacityArray = opacityArray.Trim(',');
            string onclientFunc = string.IsNullOrEmpty(this.OnClientClick) ? string.Empty : string.Format("{0}(this.url);",this.OnClientClick);

            parameters = string.Format(parameters
                                        , this.AnimationSpeed
                                        , this.AspectRatio
                                        , this.AllowButtons.ToString().ToLower()
                                        , this.AllowCaptions.ToString().ToLower()
                                        , this.Circular.ToString().ToLower()
                                        , this.AllowGlideToStartID.ToString().ToLower()
                                        , this.ImageFlowID
                                        , this.ImageFocusM
                                        , this.ImageFocusMax
                                        , this.AllowImageScaling.ToString().ToLower()
                                        , this.ImagesHeight
                                        , this.ImagesM
                                        , this.OnClientClick
                                        , this.Opacity.ToString().ToLower()
                                        , opacityArray
                                        , this.PercentLandscape
                                        , this.PercentOther
                                        , this.AllowPreloadImages.ToString().ToLower()
                                        , this.AllowReflections.ToString().ToLower()
                                        , this.ReflectionGET
                                        , this.ReflectionP
                                        , this.AllowReflectionPNG.ToString().ToLower()
                                        , this.ReflectPath
                                        , this.ScrollbarP
                                        , this.AllowSlider.ToString().ToLower()
                                        , this.SliderWidth
                                        , this.AllowSlideshow.ToString().ToLower()
                                        , this.SlideshowSpeed
                                        , this.AutoplaySlideshow.ToString().ToLower()
                                        , this.StartIndex
                                        , this.AllowAnimationOnStart.ToString().ToLower()
                                        , this.XStep);
            parameters = parameters.Replace("[OPEN]", "{");
            parameters = parameters.Replace("[CLOSED]", "}");
            initScript = initScript.Replace("[PRAMETERS]", parameters);
            this.Page.ClientScript.RegisterStartupScript(typeof(ImageFlowViewer), this.ClientID + "domReadyScript", initScript, true);
        }
        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(string.Format("<div id='{0}' class='imageflow'>", this.ImageFlowID));
            for (int i = 0; i <= 11; i++)
            {
                writer.Write("<img src=\"../Assets/Upload/reflect_dep" + i + ".jpg\"  alt=\"Image1:\" />");
            }                
            writer.Write("</div>");
        }
    }
}
